package cn.icyzx.dao;


import cn.icyzx.models.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/***
 *  默认返回类型是全部
 * Created by 郑凯 on 2016/7/18.
 */
@Repository
@Mapper
public interface UserMapper {

    @Select("select * from user where (username = #{usernameOrEmail} or email = #{usernameOrEmail}) and password = #{password}")
    User login(@Param("usernameOrEmail") String usernameOrEmail, @Param("password") String password);

    @Select("select * from user where email = #{email}")
    User getUserByEmail(@Param("email") String email);

    @Select("select * from user where username = #{username}")
    User getUserByUsername(String username);

    @Select("select * from user")
    List<User> getUserList();


    @Select("select * from user where email= #{email}")
    User existEmail(String email);

    @Select("select * from user where uid = #{uid}")
    User isActive(String uid);

    @Select("select * from user where uid = #{uid} and findPsw = #{findPsw}")
    User checkfindPsw(User user);

    @Select("select * from user where uid = #{uid}")
    User getUserByUid(@Param("uid") String uid);

    @Select("select * from Comment order by publicTime desc limit #{begin},#{len};")
    List<Comment> listDispageComments();

    @Select("select college from college")
    List<String> collegeList();

    @Select("select major from major where cid = (select cid from college where college = #{college})")
    List<String> majorList(String college);

    @Select("select classes from classes")
    List<String> classesList();


    @Insert("insert into user(uid,username,password,email) values(#{uid},#{username},#{password},#{email})")
    int insertUser(User user);

    @Insert("insert into work(wid,uid,name,classes,workImg,info,publicTime,authorName) values(#{wid},#{uid},#{name},#{classes},#{workImg},#{info},#{publicTime},#{authorName})")
    void insertWorks(Work work);

    @Insert("insert into comment(commentId,uid,wid,info,time) values(#{commentId},#{uid},#{wid},#{info},#{time})")
    void insertComments(Comment comment);

    @Update("update user set active = 'active' where uid = #{uid}")
    int activeUser(@Param("uid") String uid);

    @Update("update user set findPsw = #{findPsw} where uid = #{uid}")
    void savefindPsw(User user);

    @Update("update user set password = #{password} where uid = #{uid}")
    void modifyPsw(User user);

    @Update("update user set username = #{username},favorite = #{favorite},sign = #{sign} where uid = #{uid}")
    int updateBaseInfo(User user);

    @Update("update user set college = #{college},major = #{major},classes = #{classes} where uid = #{uid}")
    int updateCollege(@Param("uid") String uid, @Param("college") String college, @Param("major") String major, @Param("classes") String classes);

    @Update("update user set img =#{img} where uid = #{uid}")
    int updateUserAvatar(@Param("img") String img, @Param("uid") String uid);

    @Update("delete from user where uid =#{uid}")
    void deleteUserByUid(@Param("uid") String uid);
}
